import axios from '@/libs/api.request'
import { getToken } from '@/libs/util'

/** 两仓管理接口 */

/**
 * 按条件分页查询整合申报列表
 * @param {Object} data 查询条件
 */
export function queryWholeByCond (data) {
    return axios.request({
        url: '/customs/whole/queryWholeListByCond',
        data,
        method: 'post'
    })
}

/**
 * 根据cookie下载复核表(生成至文件服务器)
 * @param {String} declareId 报关单id
 * @param {String} type-->C：仓库，G：工厂
 */
export function postInvtSwPdf (declareId, type) {
    return axios.request({
        url: '/customs/whole/postInvtSwPdf',
        params: {
            param: {
                declareId,
                type
            }
        },
        method: 'post'
    })
}

/**
 * 删除整合申报
 * @param {String} declareIds  id集合 (id1,id2)
 */
export function deleteWholeById (declareIds) {
    return axios.request({
        url: '/customs/whole/deleteWholeById',
        data: {
            declareIds
        },
        method: 'post'
    })
}

/**
 * 根据id查询申报详情
 * @param {String} declareIds  id集合 (id1,id2)
 */
export function queryWholeById (declareId) {
    return axios.request({
        url: '/customs/whole/queryWholeById',
        params: {
            declareId
        },
        method: 'get'
    })
}
// 批量删除
export function deleteWholeByIds (data) {
    return axios.request({
        url: '/customs/whole/deleteWholeById',
        data,
        method: 'post'
    })
}
/**
 * 新建或修改整合申报数据--统一保存接口
 * @param {Object} data 提交数据
 */
export function unifySaveWhole (data) {
    return axios.request({
        url: '/customs/whole/unifySaveWhole',
        data,
        method: 'post'
    })
}

/**
 * 整合申报确认/取消确认（审核）
 * @param {String} declareId 报关单id
 * @param {Number} isConfirm 审核/取消审核标志
 * @param {String} continueFlag Y
 */
export function changeConfirmStatus ({ declareId, isConfirm, continueFlag = '' }) {
    return axios.request({
        url: '/customs/whole/changeConfirmStatus',
        params: {
            declareId,
            isConfirm,
            continueFlag
        },
        method: 'post'
    })
}

/**
 * 查询库存明细列表
 * @param {Object} data 查询条件
 */
export function queryInventoryByCondition (data) {
    return axios.request({
        url: '/inventory/management/queryInventoryByCondition',
        data,
        method: 'post'
    })
}

/**
 * 库存列表修改
 * @param {Object} data 修改数据
 */
export function editInventoryManagement (data) {
    return axios.request({
        url: '/inventory/management/editInventoryManagement',
        data,
        method: 'post'
    })
}

/**
 * 库存明细修改/批量修改
 * @param {Object} data 修改数据
 */
export function batchEditInventoryGoods (data) {
    return axios.request({
        url: '/inventory/management/batchEditInventoryGoods',
        data,
        method: 'post'
    })
}

/**
 * 查询库存商品信息
 * @param {String} inventoryId 库存id
 */
export function queryInventoryManagementGoods (inventoryId) {
    return axios.request({
        url: '/inventory/management/queryInventoryManagementGoods',
        params: {
            inventoryId
        },
        method: 'post'
    })
}

/**
 * 列表/明细查看修改记录
 * @param {*} param0
 */
export function queryInventoryModifyRecords ({ inventoryId, inventoryGoodsId }) {
    return axios.request({
        url: '/inventory/management/queryInventoryModifyRecords',
        params: {
            inventoryId,
            inventoryGoodsId
        },
        method: 'post'
    })
}

/**
 * 进口核注清单/入仓序号模糊查询
 *
 */
export function managementFuzzyQuery ({ importInvtNo = '', exportNo = '', inventoryGoodsNo = '', templateType = '' }) {
    return axios.request({
        url: '/inventory/management/fuzzyQuery',
        params: { importInvtNo, exportNo, inventoryGoodsNo, templateType },
        method: 'post'
    })
}

/**
 * 整合申报判断取消审核按钮是否禁用
 * @param {String} declareId 整合申报id
 */
export function checkWholeConfirmButton (declareId) {
    return axios.request({
        url: '/customs/whole/freezeConfirmButton',
        params: {
            declareId
        },
        method: 'post'
    })
}

/**
 * 删除整合申报商品集（批量）
 * @param {Object} data id结合
 */
export function deleteWholeGoodsByIds (data) {
    return axios.request({
        url: '/customs/whole/deleteWholeGoodsByIds',
        data,
        method: 'post'
    })
}

/**
 * @param {Object} data 提交数据
 *  firstQty (number, optional): 法定第一数量 ,
 *  gqty (number, optional): 数量 ,
 *  importInvtNo (string, optional): 进口核注清单编号 ,
 *  inventoryGoodsNo (string, optional): 仓库商品料号 ,
 *  secondQty (number, optional): 法定第二数量 ,
 *  transportNo (integer, optional): 出仓序号
 */
export function checkInventGoods (data) {
    return axios.request({
        url: '/inventory/management/checkInventGoods',
        data,
        method: 'post'
    })
}

/**
 * 单个商品出库
 * @param {Number} invtGoodsId 商品id
 */
export function exportInventByCond (invtGoodsId) {
    return axios.request({
        url: '/inventory/management/exportInventByCond',
        params: {
            invtGoodsId
        },
        method: 'get'
    })
}

/**
 * 导出库存到excel
 * @param {Object} data 查询条件
 */
export function exportStockExcel (data) {
    const params = Object.keys(data)
        .map(key => {
            return `${key}=${data[key] || ''}`
        }).join('&')
    window.open(
        `/inventory/management/onExportXlsGrid?Authorization=${getToken()}&${params}`
    )
}

/**
 * 导出库存到excel
 * @param {Object} data 查询条件
 */
export function exportInventExcel (data) {
    let arr = [`Authorization=${getToken()}`]
    Object.keys(data).forEach(key => {
        data[key] && arr.push(`${key}=${data[key]}`)
    })
    const params = arr.join('&')
    window.open(
        `/inventory/management/exportInventExcel?${params}`
    )
}

/**
 * 复制整合申报数据
 * @param {Number} declareId 整合申报单据id
 */
export function copyWholeDataById (declareId) {
    return axios.request({
        url: '/customs/whole/copyWholeDataById',
        method: 'get',
        params: {
            declareId
        }
    })
}

/**
 * 清空整合申报编号
 * @param {Number} declareId 报关单/核注清单id
 * @param {String} type 类型(D:报关单;G:工厂核注清单;C:仓库核注清单)
 */
export function clearWholeNo (declareId, type) {
    return axios.request({
        url: '/customs/whole/clear',
        method: 'get',
        params: {
            declareId,
            type
        }
    })
}
// 生成海运舱单
export function createSea (declareId) {
    return axios.request({
        url: '/customs/whole/createMftManifest',
        method: 'get',
        params: {
            declareId
        }
    })
}
// 刷新提单号
export function flashBillNo (declareId) {
    return axios.request({
        url: '/customs/whole/flashBillNo',
        method: 'get',
        params: {
            declareId
        }
    })
}
